<#
.SYNOPSIS
  Iconfont Generator
.DESCRIPTION
  Generate Flutter icons from iconfont.yaml
#>

# Set console encoding
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

# Find project root
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$rootDir = $scriptDir
while ($rootDir -ne $null -and -not (Test-Path "$rootDir\pubspec.yaml")) {
    $rootDir = Split-Path $rootDir -Parent
}

if (-not $rootDir) {
    Write-Host "Error: Flutter project root not found (missing pubspec.yaml)" -ForegroundColor Red
    exit 1
}

# Change to root directory
Set-Location $rootDir

# Check config file
if (-not (Test-Path "iconfont.yaml")) {
    Write-Host "Error: iconfont.yaml not found in root directory" -ForegroundColor Red
    Write-Host "Expected path: $rootDir\iconfont.yaml" -ForegroundColor Yellow
    exit 1
}

# Execute generation
Write-Host "Generating icons..." -ForegroundColor Cyan
dart run iconfont_convert --config iconfont.yaml

if ($LASTEXITCODE -eq 0) {
    Write-Host "SUCCESS: Icons generated" -ForegroundColor Green
} else {
    Write-Host "FAILED: Icon generation failed" -ForegroundColor Red
    exit 1
}